﻿--#include "data\config\modules\quests_cfg.lua" once

-- 日常任务
function packet_quest_info(sysarg, pkt, qid, qtype, qname, npc, targets, targetValues, awards)
	DataPack.writeWord(pkt, qid) -- 任务ID
	DataPack.writeByte(pkt, qtype) -- 任务类型
	DataPack.writeString(pkt, qname) -- 任务名称
	DataPack.writeByte(pkt, doCount) -- 当天当前任务已做次数
	
	if npc == nil then
		DataPack.writeInt(pkt, 0) -- NPC ID
	else
		DataPack.writeInt(pkt, npc.id) -- NPC ID
		DataPack.writeString(pkt, npc.name) -- NPC 名称
		DataPack.writeInt(pkt, npc.scene_id) -- NPC 所在场景 ID
		DataPack.writeWord(pkt, npc.x) -- NPC x
		DataPack.writeWord(pkt, npc.y) -- NPC y
	end
	
	-------------------- 目标
	if targets == nil then
		DataPack.writeByte(pkt, 0) -- 目标数量
	else
		local targetCount = #targets
		DataPack.writeByte(pkt, targetCount) -- 目标数量
		for i, target in ipairs(targets) do
			DataPack.writeByte(pkt, target.type) -- 目标类型
			DataPack.writeInt(pkt, target.id) -- 目标ID
			DataPack.writeInt(pkt, targetValues[i]) -- 目标当前值
			DataPack.writeInt(pkt, target.count) -- 目标数量
			DataPack.writeInt(pkt, target.scene_id) -- 目标场景 ID
			DataPack.writeWord(pkt, target.x) -- 目标 x
			DataPack.writeWord(pkt, target.y) -- 目标 y
			DataPack.writeString(pkt, target.entityName) -- 目标实体名称
		end
	end
	
	-------------------- 奖励
	local pos = DataPack.getDataPackPos(pkt)
	DataPack.writeByte(pkt, 0) -- 奖励项数量
	
	local awardCount = 0
	if awards ~= nil then
		for i, award in ipairs(awards) do
			local valid = true
			if award.job ~= nil and award.job ~= 0 and award.job ~= Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION) then
				valid = false
			end
			
			if valid == true and award.sex ~= nil and award.sex ~= -1 and award.sex ~= Actor.getIntProperty(sysarg,PROP_ACTOR_SEX) then
				valid = false
			end
			
			if valid == true then
				DataPack.writeByte(pkt, award.type) -- 奖励类型
				DataPack.writeWord(pkt, award.id) -- 奖励 ID
				DataPack.writeInt(pkt, target.count) -- 奖励数量
	
				awardCount = awardCount + 1
			end
			
		end
	end
	
	-- 奖励项数量
	DataPack.writeByteByPtr(pkt, pos, awardCount)
	
	--有时间限制的写个65535的进去
	DataPack.writeByte(pkt, 0)
	-- 特殊任务
	DataPack.writeByte(pkt, 0)
		
end


-- 任务次数
function expandQuestInfo(sysarg, qid, currTimes, maxTimes)
	local netPack = DataPack.allocPacket(sysarg, 6, 19) -- sQuestFinishTimesInfo
	DataPack.writeWord(netPack, qid) -- 任务ID
	DataPack.writeByte(netPack, currTimes) -- 当前完成的次数
	DataPack.writeByte(netPack, maxTimes) -- 最大次数
	DataPack.flush(netPack)
end

-- 增加可接任务
function add_can_accept_quest(sysarg, qid, qtype, qname, npcInfo, targets, awards)
	local netPack = DataPack.allocPacket(sysarg, 6, 9) -- sAddAcceptList
	DataPack.writeWord(netPack, 1) -- 数量
	packet_quest_info(sysarg, netPack, qid, qtype, qname, npcInfo, targets, awards)
	DataPack.flush(netPack)
end

-- 接受新任务
function accept_new_quest(sysarg, qid, qtype, qname, npcInfo, targets, awards)
	local netPack = DataPack.allocPacket(sysarg, 6, 2) -- sNewQuest
	packet_quest_info(sysarg, netPack, qid, qtype, qname, npcInfo, targets, awards)
	DataPack.flush(netPack)
end

-- 更新任务数据
function update_quest_data(sysarg, qid, qtype, qname, npcInfo, targets, awards)
	accept_new_quest(sysarg, qid, qtype, qname, npcInfo, targets, awards)
end

-- 放弃任务
function send_giveup_quest(sysarg, qid)
	local netPack = DataPack.allocPacket(sysarg, 6, 4) -- sGiveUpQuest
	DataPack.writeWord(netPack, qid)
	DataPack.writeByte(netPack, 0) -- 成功
	DataPack.flush(netPack)
end

-- 删除一个可接任务
function send_delete_accept_quest(sysarg, qid)
	local netPack = DataPack.allocPacket(sysarg, 6, 20) -- sDeleteAcceptQuest
	DataPack.writeWord(netPack, qid)
	DataPack.flush(netPack)
end

-- 更新任务目标值
function update_quest_target_value(sysarg, qid, target, value)
	local netPack = DataPack.allocPacket(sysarg, 6, 8) -- sSetTargetValue
	DataPack.writeWord(netPack, qid)
	DataPack.writeByte(netPack, target)
	DataPack.writeInt(netPack, value)
	
	DataPack.flush(netPack)
end

-- 发送任务完成
function send_finished_quest(sysarg, qid)
	local netPack = DataPack.allocPacket(sysarg, 6, 3) -- sFinishQuest
	DataPack.writeWord(netPack, qid)
	DataPack.writeByte(pkt, 0) -- 成功
	DataPack.flush(netPack)
end

function is_day_quest_times_finished(sysarg, qid)
	local idx = qid - 4000
	if idx < 0 or idx >= 32 then return true end
	
	local dVar = Actor.getDyanmicVar(sysarg)
	local flags = dVar.dayquestFinishedFlags
	if flags == nil then return false end
	
	local f = System.getIntBit(flags, idx)
	if (f ~= 0) then return true end
	
	return false
end

function set_day_quest_times_finished(sysarg, qid)
	local idx = qid - 4000
	if idx < 0 or idx >= 32 then return end
	
	local dVar = Actor.getDyanmicVar(sysarg)
	local flags = dVar.dayquestFinishedFlags
	if flags == nil then flags = 0 end
	
	local f = System.getIntBit(flags, idx)
	if (f ~= 0) then return end
	
	flags = System.setIntBit(flags, idx, true)
	dVar.dayquestFinishedFlags = flags
end

function Quest_OnPlayerLogin(sysarg, isCrossDay, var)
	--[[
	if var == nil then
		var = Actor.getStaticVar(sysarg)
	end
	
	-- 增加日常任务[挑战BOSS]
	add_can_accept_quest_tzboss(sysarg, var)
	
	-- 增加日常任务[休闲挂机]
	add_can_accept_quest_hook(sysarg, var)
	]]
end

function Quest_OnPlayerLevelUp(sysarg, curLevel, oldLevel)
	--[[
	local maxCfgLevel = 75 -- 日常任务需求等级的最大值
	if oldLevel >= maxCfgLevel then return end
	
	local var = Actor.getStaticVar(sysarg)
	
	-- 增加日常任务[挑战BOSS]
	cfg = quests_cfg[DailyTaskType.TYPE_TZBOSS]
	if oldLevel < cfg.needLevel and curLevel >= cfg.needLevel then
		add_can_accept_quest_tzboss(sysarg, var)
	end
	
	-- 增加日常任务[休闲挂机]
	cfg = quests_cfg[DailyTaskType.TYPE_HOOK]
	if oldLevel < cfg.needLevel and curLevel >= cfg.needLevel then
		add_can_accept_quest_hook(sysarg, var)
	end
	]]
end

--[[
各自功能，在各自文件中写
如非必要，不要在这里添加非任务相关的处理逻辑
]]
function Quest_OnMonsterByKilled(monster, sysarg, monId, fubenId, sceneId,fuBenPtr)
	--[[材料副本
	if fubenId >= 19 and fubenId <= 23 and sceneId >= 82 and sceneId <= 86 then
		local baseIdx = fubenId - 18
		
		local dVar = Actor.getDyanmicVar(sysarg)		
		
		local killCount = dVar.dayQuestFubenValue
		if killCount == nil then killCount = 0 end
		
		local dayQuestFubenId = dVar.dayQuestFubenId
		if dayQuestFubenId == nil or dayQuestFubenId ~= fubenId then
			killCount = 0
			dVar.dayQuestFubenId = fubenId
		end
		
		killCount = killCount + 1
		dVar.dayQuestFubenValue = killCount
		
		local qid = DailyTaskType.TYPE_CLFB
		local targetIdx = 0	
		update_quest_target_value(sysarg, qid, targetIdx, killCount)
	end
	]]
end

-- 在线跨天
function Quest_OnActorEventNewDayAcrrive(sysarg)
	--[[
	if var == nil then
		var = Actor.getStaticVar(sysarg)
	end
	
	local dVar = Actor.getDyanmicVar(sysarg)
	local flags = dVar.dayquestFinishedFlags
	if flags == nil then flags = 0 end
	
	-- 增加日常任务[挑战BOSS]
	f = System.getIntBit(flags, 5)
	if f ~= 0 then
		add_can_accept_quest_tzboss(sysarg, var)
	end
	
	-- 增加日常任务[休闲挂机]  一直存在
	--add_can_accept_quest_hook(sysarg, var)
	
	-- 清0
	dVar.dayquestFinishedFlags = 0
	]]
end

function GetDayQuestChuansongPointId(sysarg, qid)		
	-- data\config\misc\ChuansongPoint.lua
	if qid == DailyTaskType.TYPE_FSYB then -- 护送押镖
		local var = Actor.getStaticVar(sysarg)
		if var.ybstate ~= nil and var.ybstate == 1 then
			return -- 押镖时不能传送
		end
	end
	
	local qcfg = quests_cfg[qid]
	if qcfg == nil then return end
	
	return qcfg.chuansongPointId	
end

--------------------------- 降妖除魔 ---------------------------
-- 获得降妖除魔完成次数
function get_xycm_finish_count(sysarg, var)
	if var == nil then
		var = Actor.getStaticVar(sysarg)
	end

	if not var.days then var.days = {} end
	
	local nCount = 0;
	local freeXYCHM = var.days.freeXYCHM -- 免费使用了多少次降妖除魔
	if freeXYCHM ~= nil then
		nCount = nCount + freeXYCHM;
	end
	
	return nCount
end

function accept_new_quest_fuben(sysarg, fubenIdx)
	local qid = DailyTaskType.TYPE_CLFB
	local cfg = quests_cfg[qid]
	local fbcfg = cfg.fubens[fubenIdx]
	if fbcfg == nil then return end
	
	local targetValues = {0}
	accept_new_quest(sysarg, qid, 3, cfg.name, cfg.npc, fbcfg.target, targetValues, fbcfg.awards)
end


--------------------------- 挑战BOSS ---------------------------
-- 增加日常任务[挑战BOSS]
function add_can_accept_quest_tzboss(sysarg, var)
	if var == nil then
		var = Actor.getStaticVar(sysarg)	
	end
	
	--local actorLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)	
	
	local qid = DailyTaskType.TYPE_TZBOSS
	local cfg = quests_cfg[qid]
	if not Actor.checkActorLevel(sysarg, cfg.needLevel, 0) then return end
	
	add_can_accept_quest(sysarg, qid, 3, cfg.name, cfg.npc)
end

--------------------------- 休闲挂机 ---------------------------
-- 增加日常任务[休闲挂机]
function add_can_accept_quest_hook(sysarg, var)
	if var == nil then
		var = Actor.getStaticVar(sysarg)	
	end

	--local actorLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)	
		
	local qid = DailyTaskType.TYPE_HOOK
	local cfg = quests_cfg[qid]
	if not Actor.checkActorLevel(sysarg, cfg.needLevel, 0) then return end
	
	add_can_accept_quest(sysarg, qid, 3, cfg.name, cfg.npc)
end